﻿using System;
using System.Collections.Generic;
using System.Linq;

using TempEmployees.DAL;
using TempEmployees.BLL.EntitiesExtensions;
using TempEmployees.BLL.Database; 

namespace TempEmployees.BLL.Budgets
{
    public class AssignmentBalance
    {
        public Assignment Assignment { get; private set; }
        public Semester Semester { get; private set; }

        private List<AnamReport> m_anamReports = null;
        private List<AnamReport> AnamReports
        {
            get
            {
                if (m_anamReports == null)
                {
                    m_anamReports = new List<AnamReport>();
                    DbQuery.Database.AnamReports.Where(r =>
                    r.AssignmentID == Assignment.ID
                    ).ToList().ForEach(r =>
                    {
                        if (ESemester.IsInSemester(Semester, r.RelevantDate)) m_anamReports.Add(r);
                    });
                }
                return m_anamReports;
            }
        }

        public AssignmentBalance(Assignment assignment, Semester semester)
        {
            Assignment = assignment;
            Semester = semester;
        }

        public double GetStaticCost()
        {
            return (double)Assignment.GetSalary() * GetStaticHours();
        }

        public double GetStaticHours()
        {
            return Assignment.MonthlyHours * Assignment.GetLengthInSemester(Semester);
        }

        public double GetCurrentCost()
        {
            double sum = 0;
            AnamReports.ForEach(r => 
                sum += ( (double)r.WorkingHours * (double)r.Salary ) 
                );

            return sum;
        }

        public double GetCurrentBalance()
        {
            return GetStaticCost() - GetCurrentCost();
        }

        public double GetCurrentHours()
        {
            double hours = 0;
            AnamReports.ForEach(r =>
                hours += (double)r.WorkingHours
                );

            return hours;
        }

        public double GetCurrentHoursBalance()
        {
            return GetStaticHours() - GetCurrentHours();
        }
    }
}